package com.um.swing.ui;

import com.um.swing.common.inc.Constant;
import sun.swing.SwingUtilities2;

import javax.swing.*;
import javax.swing.plaf.basic.BasicGraphicsUtils;
import javax.swing.plaf.basic.BasicTabbedPaneUI;
import javax.swing.text.View;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class ITabbedPaneUI extends BasicTabbedPaneUI {

    private static final int mTabBottom = 32;

    public ITabbedPaneUI(String bgColorHex, String frontColorHex) {
    }


    @Override
    public void installUI(JComponent c) {
        super.installUI(c);
        c.setOpaque(true);
        RollOverListener l = new RollOverListener();
        tabPane.addMouseListener(l);
        tabPane.addMouseMotionListener(l);
    }


    @Override
    protected void paintTabArea(Graphics g, int tabPlacement, int selectedIndex) {
        Graphics2D g2d = (Graphics2D) g;
        g.setColor(Constant.MAIN_BG_COLOR);
        g.fillRect(0, 0, tabPane.getWidth(), mTabBottom);
        g.setColor(Constant.COLOR_BORDER_BLACK);
        g.drawLine(0, mTabBottom - 1, tabPane.getWidth(), mTabBottom - 1);
        super.paintTabArea(g, tabPlacement, selectedIndex);
    }

    @Override
    protected void paintTab(Graphics g, int tabPlacement, Rectangle[] rects, int tabIndex, Rectangle iconRect, Rectangle textRect) {
//        super.paintTab(g, tabPlacement, rects, tabIndex, iconRect, textRect);
        Graphics2D g2d = (Graphics2D) g;
        Rectangle tabRect = rects[tabIndex];
        int selectedIndex = tabPane.getSelectedIndex();
        boolean isSelected = selectedIndex == tabIndex;

        if (isSelected) {
            g2d.setColor(Constant.COLOR_TAB_ACTIVE);
        } else {
            g2d.setColor(Constant.COLOR_TAB_BG);
        }
        g2d.fillRect(tabRect.x, 0, tabRect.width, mTabBottom - 1);

        g2d.setColor(Constant.COLOR_TAB_RIGHT_LINE);
        int right = tabRect.x + tabRect.width;
        g2d.setStroke(new BasicStroke(1));
        g2d.drawLine(right, 0, right, mTabBottom - 2);
        g2d.drawLine(tabRect.x, 0, tabRect.x, mTabBottom - 2);
        g2d.setFont(Constant.FONT_BASE_14);
        //绘制文字
        if (isSelected) {
            g2d.setColor(Constant.COLOR_TEXT_WHITE);
        } else {
            g2d.setColor(Constant.COLOR_TEXT_DEF);
        }
        int mnemIndex = tabPane.getDisplayedMnemonicIndexAt(tabIndex);
        String title = tabPane.getTitleAt(tabIndex);
        Font font = tabPane.getFont();
        FontMetrics metrics = SwingUtilities2.getFontMetrics(tabPane, g2d, font);

        layoutLabel(tabPlacement, metrics, tabIndex, title, null, tabRect, iconRect, textRect, isSelected);
        BasicGraphicsUtils.drawStringUnderlineCharAt(g2d, title, mnemIndex,
                textRect.x, textRect.y + metrics.getAscent());
    }

    @Override
    protected void paintText(Graphics g, int tabPlacement, Font font,
                             FontMetrics metrics, int tabIndex, String title, Rectangle textRect, boolean isSelected) {
        super.paintText(g, tabPlacement, font, metrics, tabIndex, title,
                textRect, isSelected);
        g.setFont(font);
        View v = getTextViewForTab(tabIndex);
        if (v != null) {
            // html
            v.paint(g, textRect);
        } else {
            // plain text
            int mnemIndex = tabPane.getDisplayedMnemonicIndexAt(tabIndex);
            if (tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex)) {
                if (isSelected) {
                    g.setColor(Color.decode("#ffffff"));
                } else {
                    g.setColor(Color.decode("#908a8d"));
                }
                BasicGraphicsUtils.drawStringUnderlineCharAt(g, title, mnemIndex,
                        textRect.x, textRect.y + metrics.getAscent());
            } else { // tab disabled
                g.setColor(Color.BLACK);
                BasicGraphicsUtils
                        .drawStringUnderlineCharAt(g, title, mnemIndex,
                                textRect.x, textRect.y + metrics.getAscent());
                g.setColor(tabPane.getBackgroundAt(tabIndex).darker());
                BasicGraphicsUtils.drawStringUnderlineCharAt(g, title,
                        mnemIndex, textRect.x - 1,
                        textRect.y + metrics.getAscent() - 1);
            }
        }
    }

    //固定tab标签宽度
    @Override
    protected int calculateTabWidth(int tabPlacement, int tabIndex,
                                    FontMetrics metrics) {
        return 120;
        //      return 10 + anchoFocoV
        //              + super.calculateTabWidth(tabPlacement, tabIndex, metrics);
    }


    @Override
    protected int calculateTabHeight(int tabPlacement, int tabIndex,
                                     int fontHeight) {

        return super.calculateTabHeight(tabPlacement, tabIndex, fontHeight);
    }

    protected int calculateMaxTabHeight(int tabPlacement) {
        return 25;
    }

    protected int getTabLabelShiftY(int tabPlacement, int tabIndex,
                                    boolean isSelected) {
        return 0;
    }

    protected int getTabLabelShiftX(int tabPlacement, int tabIndex,
                                    boolean isSelected) {
        return 0;
    }

    @Override
    protected void paintFocusIndicator(Graphics g, int tabPlacement,
                                       Rectangle[] rects, int tabIndex, Rectangle iconRect,
                                       Rectangle textRect, boolean isSelected) {
        if (tabPane.hasFocus() || isSelected) {
            // g.setColor(UIManager.getColor("ScrollBar.thumbShadow"));
            // g.drawPolygon(shape);
        }
    }


    private int lastRollOverTab = -1;

    private class RollOverListener implements MouseMotionListener,
            MouseListener {

        @Override
        public void mouseClicked(MouseEvent e) {
        }

        @Override
        public void mousePressed(MouseEvent e) {

        }

        @Override
        public void mouseReleased(MouseEvent e) {

        }

        @Override
        public void mouseDragged(MouseEvent e) {

        }

        @Override
        public void mouseMoved(MouseEvent e) {
            checkRollOver();
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            checkRollOver();
        }

        @Override
        public void mouseExited(MouseEvent e) {
            tabPane.repaint();
        }

        private void checkRollOver() {
            int currentRollOver = getRolloverTab();
            if (currentRollOver != lastRollOverTab) {
                lastRollOverTab = currentRollOver;
                Rectangle tabsRect = new Rectangle(0, 0, tabPane.getWidth(),
                        tabPane.getHeight() + 1);
                tabPane.repaint(tabsRect);
            }
        }
    }
}
